﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;

public partial class Stats : System.Web.UI.Page
{
    List<string> teamNames;
    List<Player> players;


    protected void Page_Init(object sender, EventArgs e)
    {

        addTeamsToDropdown();
        addPositionsToDropdown();
    }      
    protected void Page_Load(object sender, EventArgs e)
    {
        teamNames = new List<string>();
        JsonParser jsonParser = new JsonParser();
        players = jsonParser.getAllPlayers();
        if (players != null)
        {
            initPlayers(players);
            list(players);
        }
    }

    protected void onTeam_Selected(object sender, EventArgs e)
    {
        string selectedTeam = ((DropDownList)sender).SelectedValue.ToString();
        string selectedPos = PositionDD.SelectedValue.ToString();
        PlayerPanel.Controls.Clear();
        if (!selectedTeam.Equals("all"))
        {
            if (selectedPos.Equals("all"))
            {
                Team team = (Team)Enum.Parse(typeof(Team), selectedTeam, true);
                list(players, team);
            }
            else
            {
                Team team = (Team)Enum.Parse(typeof(Team), selectedTeam, true);
                Position position = (Position)Enum.Parse(typeof(Position), selectedPos, true);
                list(players, team, position);
            }
        }
        else
        {

            if (selectedPos.Equals("all"))
            {

                list(players);
            }
            else
            {
                Position position = (Position)Enum.Parse(typeof(Position), selectedPos, true);
                list(players, position);
            }
        }
    }

    protected void onPosition_Selected(object sender, EventArgs e)
    {
        string selectedPos = ((DropDownList)sender).SelectedValue.ToString();
        string selectedTeam = TeamDD.SelectedValue.ToString();
        PlayerPanel.Controls.Clear();
        if (!selectedPos.Equals("all"))
        {
            if (selectedTeam.Equals("all"))
            {
                Position pos = (Position)Enum.Parse(typeof(Position), selectedPos, true);
                list(players, pos);
            }
            else
            {
                Team team = (Team)Enum.Parse(typeof(Team), selectedTeam, true);
                Position position = (Position)Enum.Parse(typeof(Position), selectedPos, true);
                list(players, team, position);
            }
        }
        else
        {
            if (selectedTeam.Equals("all"))
            {
                list(players);
            }
            else
            {
                Team team = (Team)Enum.Parse(typeof(Team), selectedTeam, true);
                list(players, team);
            }
        }
    }

    private void initPlayers(List<Player> players)
    {
        foreach (Player player in players)
        {
            player.setRealTeam();
        }
    }

    private void list(List<Player> players)
    {
        foreach (Player player in players)
        {
            Label myLabel = new Label();
            myLabel.Text = player.playerName;

            PlayerPanel.Controls.Add(myLabel);
            PlayerPanel.Controls.Add(new LiteralControl("<br />"));
        }
        PlayerPanel.Controls.Add(new LiteralControl("<br />"));
    }

    private void list(List<Player> players, Position position)
    {
        foreach (Player player in players)
        {
            if (player.position.Equals(position))
            {
                Label myLabel = new Label();
                myLabel.Text = player.playerName;

                PlayerPanel.Controls.Add(myLabel);
                PlayerPanel.Controls.Add(new LiteralControl("<br />"));
            }
        }
        PlayerPanel.Controls.Add(new LiteralControl("<br />"));
    }

    private void list(List<Player> players, Team team)
    {

        foreach (Player player in players)
        {
            if (player.team.Equals(team))
            {
                Label myLabel = new Label();
                myLabel.Text = player.playerName;

                PlayerPanel.Controls.Add(myLabel);
                PlayerPanel.Controls.Add(new LiteralControl("<br />"));
            }
        }
        PlayerPanel.Controls.Add(new LiteralControl("<br />"));
    }

    private void list(List<Player> players, Team team, Position position)
    {

        foreach (Player player in players)
        {
            if (player.team.Equals(team) && player.position.Equals(position))
            {
                Label myLabel = new Label();
                myLabel.Text = player.playerName;

                PlayerPanel.Controls.Add(myLabel);
                PlayerPanel.Controls.Add(new LiteralControl("<br />"));
            }
        }
        PlayerPanel.Controls.Add(new LiteralControl("<br />"));
    }

    private void addTeamsToDropdown()
    {
        Array itemValues = System.Enum.GetValues(typeof(Team));
        Array itemNames = System.Enum.GetNames(typeof(Team));

        ListItem item2 = new ListItem("All teams", "all");
        TeamDD.Items.Add(item2);

        for (int i = 0; i <= itemNames.Length - 1 ; i++) {
            ListItem item = new ListItem(removeUnderscore(itemNames.GetValue(i).ToString()), itemValues.GetValue(i).ToString());
            TeamDD.Items.Add(item);
        }
    }

    private void addPositionsToDropdown()
    {
        Array itemValues = System.Enum.GetValues(typeof(Position));
        Array itemNames = System.Enum.GetNames(typeof(Position));

        ListItem item2 = new ListItem("All players", "all");
        PositionDD.Items.Add(item2);

        for (int i = 0; i <= itemNames.Length - 1; i++)
        {
            ListItem item = new ListItem(removeUnderscore(itemNames.GetValue(i).ToString()), itemValues.GetValue(i).ToString());
            PositionDD.Items.Add(item);
        }
    }

    private string removeUnderscore(string st)
    {
        st = st.Replace('_', ' ');
        return st;
    }
}
